﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BusinessLogicLayer;
using Entities;

namespace QuanLyTKB_TienDoGD.QuanLyMonHoc
{
    public partial class frmAddMonHoc : Form
    {
        /// <summary>
        /// Tạo đối tượng _manager
        /// </summary>
        private MonHocManager _manager;
        /// <summary>
        /// tạo đối tượng môn học
        /// </summary>
        private MonHoc _monhoc;
        /// <summary>
        /// Tạo form frmThemLopHoc
        /// </summary>
        private bool _isUpdate;
        /// <summary>
        /// tạo đối tượng cha từ form frmQuanLyLopHoc
        /// </summary>
        frmQLMonHoc cha;
        /// <summary>
        /// Tạo form frmAddMonHoc
        /// </summary>
        public frmAddMonHoc()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Phương thức tạo lập frmThemMonHoc
        /// </summary>
        /// <param name="_man"></param>
        /// <param name="frmCha"></param>
        /// <param name="_lh"></param>
        public frmAddMonHoc(MonHocManager _man, frmQLMonHoc p, MonHoc _mh = null)
            : this()
        {
            _manager = _man;
            _monhoc = _mh;
            cha = p;
            _isUpdate = (_mh != null);
        }
        /// <summary>
        /// Hàm load form frmThemMonHoc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmAddMonHoc_Load(object sender, EventArgs e)
        {
            if (_isUpdate)
            {
                txtMaMH.Text = _monhoc.MaMH;
                txtTenMH.Text = _monhoc.TenMH;
                chkBB.Checked = _monhoc.BatBuoc;
                nudSTCLT.Value = _monhoc.SoTCLT;
                nudSTCTH.Value = _monhoc.SoTCTH;
            }
            txtMaMH.ReadOnly = _isUpdate;
        }
        /// <summary>
        /// Hàm save môn học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_isUpdate) _monhoc = new MonHoc();

            _monhoc.MaMH = txtMaMH.Text;
            _monhoc.TenMH = txtTenMH.Text;
            _monhoc.BatBuoc = chkBB.Checked;
            _monhoc.SoTCLT = (int)nudSTCLT.Value;
            _monhoc.SoTCTH = (int)nudSTCTH.Value;

            var vResult = _manager.Validate(_monhoc);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(_monhoc);
                if (aResult.Success)
                {
                    try { cha.btnLamMoi.PerformClick(); }
                    catch (Exception) { }
                    MessageBox.Show((_isUpdate ? "Cập nhật" : "Thêm") + " thành công thông tin môn học", "Thông báo");
                    btnReset.PerformClick();
                }
                else
                    MessageBox.Show(aResult.GetErrosMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }
        /// <summary>
        /// Hàm thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Hàm reset
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaMH.Text = string.Empty;
            txtTenMH.Text = string.Empty;
            chkBB.Checked = false;
            nudSTCLT.Value = 0;
            nudSTCTH.Value = 0;
            _isUpdate = false;
            this.Text = "Thêm môn học";
            txtMaMH.ReadOnly = _isUpdate;
        }

  
  
    }
}
